package com.wlsk.license.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.wlsk.license.common.constant.RedisConstant;
import com.wlsk.license.common.constant.RedisService;
import com.wlsk.license.common.exception.BusinessException;
import com.wlsk.license.common.result.Result;
import com.wlsk.license.common.utils.ValidationUtils;
import com.wlsk.license.dto.request.feign.GetOssObjectUrlReqDto;
import com.wlsk.license.dto.response.feign.OssObjectInfoResultDto;
import com.wlsk.license.service.IFileUploadService;
import com.wlsk.license.service.feign.PlatOssFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class FileUploadServiceImpl implements IFileUploadService {
    @Resource
    private PlatOssFeignClient platOssFeignClient;
    /**
     * OSS上传位置
     */
    private static final int POSITION = 15;

    @Override
    public String getFile(Long fileId) {
        String url = "";
        RedisService redisService = SpringUtil.getBean(RedisService.class);

        Object fileObject = redisService.getCacheObject(RedisConstant.FILE_URL_KEY + fileId);
        try {
            if (ObjectUtils.isEmpty(fileObject)) {
                Result<OssObjectInfoResultDto> result = platOssFeignClient.getOssObjectUrl(new GetOssObjectUrlReqDto(fileId));
                ValidationUtils.isTrue(result.isSuccess(), result.getMsg());
                url = result.getData().getUrl();
                redisService.setCacheObject(RedisConstant.FILE_URL_KEY + fileId, url, 12L, TimeUnit.HOURS);
            } else {
                url = String.valueOf(fileObject);
            }
        } catch (Exception e) {
            log.error("获取文件url调用通用文件服务错误:{}", e.getMessage(), e);
            //throw new BusinessException("获取文件url错误");
        }
        return url;
    }

    @Override
    public List<String> getFile(List<Long> fileIds) {
        List<String> urls = new ArrayList<>(fileIds.size());
        RedisService redisService = SpringUtil.getBean(RedisService.class);


        for (Long fileId : fileIds) {
            String cacheKey = RedisConstant.FILE_URL_KEY + fileId;
            String url = (String) redisService.getCacheObject(cacheKey);

            if (ObjectUtils.isEmpty(url)) {

                try {
                    Result<OssObjectInfoResultDto> result = platOssFeignClient.getOssObjectUrl(new GetOssObjectUrlReqDto(fileId));
                    ValidationUtils.isTrue(result.isSuccess(), result.getMsg());
                    url = result.getData().getUrl();

                    redisService.setCacheObject(cacheKey, url, 12L, TimeUnit.HOURS);
                } catch (Exception e) {
                    log.error("获取文件url调用通用文件服务错误, fileId: {}: {}", fileId, e.getMessage(), e);
                    throw new BusinessException("获取文件url错误");
                }
            }

            // 将获取到的 URL 加入到列表
            urls.add(url);
        }

        return urls;
    }

    @Override
    public Result<OssObjectInfoResultDto> uploadOss(MultipartFile ossObject) {
        Result<OssObjectInfoResultDto> result = platOssFeignClient.uploadOssObject(ossObject, POSITION);
        ValidationUtils.isTrue(result.isSuccess(), result.getMsg());
        result = platOssFeignClient.getOssObjectUrl(new GetOssObjectUrlReqDto(result.getData().getId()));
        ValidationUtils.isTrue(result.isSuccess(), "上传异常");
        return result;
    }
}
